/*
 * Created on Sep 19, 2008 
 *  
 * COPYRIGHT (C) 2008, ZyXEL Corporation (WuXi) Co., Ltd                  
 * Protected as an unpublished work. All Rights Reserved.
 * ZYXEL PROPRIETARY/CONFIDENTIAL.                                               
 * 
 * The computer program listings, specifications, and 
 * documentation herein are the property of ZYXEL 
 * Corporation and shall not be reproduced, copied, 
 * disclosed, or used in whole or in part for any reason 
 * without the prior express written permission of     
 * ZYXEL Corporation.                                    
 */
package cnm.validator.xpath.routing;

import java.util.Arrays;
import java.util.List;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import cnm.validator.xpath.XPathValidator;

/**
 * 
 * @author Harry
 * @version
 *
 */
public class PolicyRouteXPath extends XPathValidator {

    @Override
    public void xpathCheck() {
        logger.debug("start PolicyRoute XPath validate...");
        
        String[] nexthop_types = {"Auto", "Trunk", "VPN Tunnel", "Gateway", "Interface"};
        final List<String> NEXTHOP_TYPE_LIST = Arrays.asList(nexthop_types);
        
        XPathFactory xf = XPathFactory.newInstance();
        XPath path = xf.newXPath();
        Document doc = createDoc();
        try {
            checkNextHopType(NEXTHOP_TYPE_LIST, path, doc);
            
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        }
        
    }

    private void checkNextHopType(final List<String> NEXTHOP_TYPE_LIST,
            XPath path, Document doc) throws XPathExpressionException {
        XPathExpression expreOfNextHop = path.compile("//DATASET/ATTRIBUTE[@name='nexthop_type']/text()");
        NodeList nodes = (NodeList) expreOfNextHop.evaluate(doc, XPathConstants.NODESET);
        
        if (nodes == null ||nodes.getLength() == 0) {
            logger.debug("[validate fail]: can't find 'nexthop_type'");
        } else {
            for (int i = 0; i < nodes.getLength(); i++) {
                String value = nodes.item(i).getNodeValue();
                if (!NEXTHOP_TYPE_LIST.contains(value)) {
                    if(logger.isDebugEnabled()) {
                        logger.debug("[Validate fail]: " + value + " is not supported");
                    }
                }
            }
        }
    }

}
